//
//  AppDelegate+AppService.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/4.
//

#import "AppDelegate+AppService.h"
#import "RHTabBarController.h"
#import "RHLoginViewController.h"
#import "RHNavigationController.h"
#import "EnvironmentManage.h"
#import "NetworkConfig.h"
#import "LocationManager.h"
#import <MJRefresh/MJRefreshConfig.h>
#import "RHLaunchVC.h"
//#import <Bugly/Bugly.h>

@implementation AppDelegate (AppService)

+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}


//MARK:————— 初始化服务 —————
-(void)initService{
//    [Bugly startWithAppId:@"d36e40026d"];
    if (Environment) {//线上环境
        
    }
    [NetworkConfig sharedConfig].baseUrl = [[EnvironmentManage sharedEnvirManage] getBaseAPIUrl];
    [NetworkConfig sharedConfig].brandID = brand_id;
//    if([UserDefaults objectForKey:kBrandID]){
//        [NetworkConfig sharedConfig].brandID = [UserDefaults objectForKey:kBrandID];
//    }else{
//        [NetworkConfig sharedConfig].brandID = brand_id;
//    }
    
    if([PrivateMange share].isLogined){
        [NetworkConfig sharedConfig].baseUrl = [UserDefaults objectForKey:kServerApi];
    }
    //获取地理位置
    [[LocationManager manager] getLocation:^(CLLocationCoordinate2D coordinate) {}];
    
    
    [[AFNetworkReachabilityManager sharedManager]startMonitoring];
}

//MARK:————— 初始化window —————
-(void)initWindow{
    
    self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    RHLaunchVC *lunchVC = [[RHLaunchVC alloc]init];
    self.window.rootViewController = lunchVC;
    kWeakSelf;
    lunchVC.beginAction = ^{
        if([PrivateMange share].isLogined){

            //已登录过  更新品牌
            [QMUITips showLoadingInView:[UIApplication sharedApplication].keyWindow];
            [[PrivateMange share]getUserInfoSuccess:^(RHUserModel * _Nonnull userModel) {
                RHTabBarController *tabController = [[RHTabBarController alloc] init];
                tabController.hidesBottomBarWhenPushed = NO;
                weakSelf.window.rootViewController = tabController;
            }];
            
//            if( [[PrivateMange share] readUserInfo]){
//                if([PrivateMange share].currentUser.brandId == 0){
//                    RHLoginViewController *Login_vc = [[RHLoginViewController alloc] init];
//                    RHNavigationController *login_nav = [[RHNavigationController alloc] initWithRootViewController:Login_vc];
//                    [weakSelf.window setRootViewController:login_nav];
//                    return;
//                }
//                
//            }else{
//                RHLoginViewController *Login_vc = [[RHLoginViewController alloc] init];
//                RHNavigationController *login_nav = [[RHNavigationController alloc] initWithRootViewController:Login_vc];
//                [weakSelf.window setRootViewController:login_nav];
//            }

        }else{
            RHLoginViewController *Login_vc = [[RHLoginViewController alloc] init];
            RHNavigationController *login_nav = [[RHNavigationController alloc] initWithRootViewController:Login_vc];
            [weakSelf.window setRootViewController:login_nav];
        }
    };
    
    
    
}

#pragma MARK ------ 配置语言
-(void)setupLanguage
{
   
    //当前语言
    NSString *selectLanguage = [NSString getCurrentLanguage];
   
    //除了中文,其他一概设成英文
    if ([selectLanguage containsString:@"zh"]) {//中文
        [self setLanguage: @"zh-Hans"];
    }else if([selectLanguage containsString:@"en"]){//英语
        [self setLanguage:@"en"];
    }
    else if([selectLanguage containsString:@"fr"]){//法语
        [self setLanguage:@"fr"];
    }else if([selectLanguage containsString:@"de"]){//德语
        [self setLanguage:@"de"];
    }else if([selectLanguage containsString:@"ja"]){//日语
        [self setLanguage:@"ja"];
    }else if([selectLanguage containsString:@"pl"]){//波兰语
        [self setLanguage:@"pl"];
    }
//    else if([selectLanguage containsString:@"it"]){//意大利语
//        [self setLanguage:@"it"];
//    }
    else if([selectLanguage containsString:@"es"]){//西班牙语
        [self setLanguage:@"es"];
    }
    
    else{
        [self setLanguage:@"en"];//不在配置列表内的显示英文
    }

}
-(void)setLanguage:(NSString*)lan
{
    [kUserDefaults setObject:lan forKey:kLanguage];
    
    [NSBundle setLanguage:lan];
    //设置下拉刷新语言
    [MJRefreshConfig defaultConfig].languageCode = lan;
    NSLog(@"----->设置语言为:%@",lan);
    
    //后台语言
    [NetworkConfig sharedConfig].language = lan;
    
    if([lan containsString:@"zh"]){
        //图片选择器的语言
        [[NSUserDefaults standardUserDefaults] setObject:@(ZLLanguageChineseSimplified) forKey:@"ZLLanguage"];
    }else{
        //图片选择器的语言
        [[NSUserDefaults standardUserDefaults] setObject:@(ZLLanguageEnglish) forKey:@"ZLLanguage"];
    }
    
}

@end
